<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 11:30 AM
 * To change this template use File | Settings | File Templates.
 */
// No direct access.
defined('_JEXEC') or die;

// Include dependancy of the main controllerform class
jimport('joomla.application.component.controllerform');

class VastControllerHuongnghiencuu extends JControllerForm
{

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

    public function create()
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();
        $model  = $this->getModel('huongnghiencuu');

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');

        $form = $model->getForm();
        
        if (!$form) {
            JError::raiseError(500, $model->getError());
            return false;
        }        
        
        $validate = $model->validate($form, $data);

        // Check for validation errors.
        if ($validate === false) {
            // Get the validation messages.
            $errors = $model->getErrors();

            // Push up to three validation messages out to the user.
            for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
                if (JError::isError($errors[$i])) {
                  $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
                } else {
                  $app->enqueueMessage($errors[$i], 'warning');
                }
            }

            // Redirect back to the previous screen.
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=huongnghiencuu&layout=default&id=' . $data['hosocanbo_id'], false));
            return false;
        }

        // Now update the loaded data to the database via a function in the model
        $createitem = $model->createItem($data);
        
        // check if ok and display appropriate message.  This can also have a redirect if desired.
        if ($createitem) {
            $this->setMessage('Hướng nghiên cứu đã được thêm vào thành công');
            $this->setRedirect(JRoute::_('index.php?option=com_vast&context=quanlyhuongnghiencuu&view=quanlyhuongnghiencuu&id=' . $data['hosocanbo_id']));
        } else {
            // Get a handle to the Joomla! application object
            $application = JFactory::getApplication();
 
            // Add a message to the message queue
            $application->enqueueMessage('Thông tin hướng nghiên cứu đã được thêm vào không thành công có lỗi đã xảy ra!', 'error');
            // Redirect back to the previous screen.
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=huongnghiencuu&layout=default&id=' . $data['hosocanbo_id'], false));
            return false;
        }

        return true;
    }

    public function update()
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();
        $model  = $this->getModel('huongnghiencuu');

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');

        $form = $model->getForm();
        
        if (!$form) {
            JError::raiseError(500, $model->getError());
            return false;
        }        
        
        $validate = $model->validate($form, $data);

        // Check for validation errors.
        if ($validate === false) {
            // Get the validation messages.
            $errors = $model->getErrors();

            // Push up to three validation messages out to the user.
            for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
                if (JError::isError($errors[$i])) {
                  $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
                } else {
                  $app->enqueueMessage($errors[$i], 'warning');
                }
            }

            // Redirect back to the previous screen.
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=huongnghiencuu&layout=edit&id=' . $data['hosocanbo_id'], false));
            return false;
         }

        // Now update the loaded data to the database via a function in the model
        $updateitem = $model->updateItem($data);
        
        // check if ok and display appropriate message.  This can also have a redirect if desired.
        if ($updateitem) {
            $this->setMessage('Hướng nghiên cứu đã được cập nhật thành công');
            $this->setRedirect(JRoute::_('index.php?option=com_vast&context=quanlyhuongnghiencuu&view=quanlyhuongnghiencuu&id=' . $data['hosocanbo_id']));
        } else {
            // Get a handle to the Joomla! application object
            $application = JFactory::getApplication();
 
            // Add a message to the message queue
            $application->enqueueMessage('Thông tin hướng nghiên cứu đã được cập nhật không thành công có lỗi đã xảy ra!', 'error');
            // Redirect back to the previous screen.
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=huongnghiencuu&layout=edit&id=' . $data['hosocanbo_id'], false));
            return false;
        }

        return true;
    }

}